﻿<!--日期段选择模块，必须引用-->
<table align="left" style="width:300px; left: -3px">
     <tr>
        <td width ="70px">
            <input class="mini-datepicker" name="FDate1" format="MM-dd" style="width:70px;" 
                value = ""  onValueChanged="onValueChanged" showClearButton="false"/></td>
        <td width ="16px">至</td>
        <td width="70px">
            <input class="mini-datepicker" name="FDate3" format="MM-dd" style="width:70px;"  showTodayButton="false" showClearButton="false"
                   value=""  onDrawDate="onDrawDate" onValueChanged="onValueChanged3" onBeforeshowpopup ="onBeforeshowpopup3"/>
            <input class="mini-datepicker" name="FDate2" format="MM-dd" style="width:70px;" value="" visible="false" />
        </td>
        <td width ="70px">
            <input id="comdatasel" name="comdatasel" class="mini-combobox" style="width:70px;" value = "0"showNullItem="false" 
               required="true" allowInput="false"onvaluechanged="onComDataselChanged" align="left" data ="datasel"/>
        </td>
     </tr>
</table>
<script  type="text/javascript">

    var datasel = [{ "id": "0", "text": "今天" },
                          { "id": "1", "text": "本月" },
                          { "id": "2", "text": "上月" },
                          { "id": "3", "text": "昨天" },
                          //{ "id": "4", "text": "本周" },
                          { "id": "5", "text": "本季度" },
                          { "id": "6", "text": "本年度"}]
    $(function () {
        var nowDate = new Date();
        mini.getbyName("FDate1").setValue(nowDate);
        mini.getbyName("FDate2").setValue(nowDate);
        mini.getbyName("FDate3").setValue(mini.getbyName("FDate2").getValue());
    });

    function onValueChanged(e) {
        var d = mini.getbyName("FDate3").getValue();
        d.setFullYear(mini.getbyName("FDate1").getValue().getFullYear());
        mini.getbyName("FDate3").setValue(d);
        mini.getbyName("FDate2").setValue(mini.getbyName("FDate3").getValue());
    } 

    function onValueChanged3() {
        var date1 = mini.getbyName("FDate1").getValue();
        var date2 = date1.getFullYear() + "-" + mini.getbyName("FDate3").getFormValue();
        mini.getbyName("FDate2").setValue(date2);
    }

    function onBeforeshowpopup3(e) {
        mini.getbyName("FDate3").setValue(mini.getbyName("FDate2").getFormValue());
    }  

    function onDrawDate(e) {
        var date = e.date;
        var d = mini.getbyName("FDate1").getValue();

        if (date.getFullYear() != d.getFullYear()) {
                e.allowSelect = false;
        }
    }

    //获得本季度的开始月份     
    function getQuarterStartMonth(nowMonth) {
        var quarterStartMonth = 0;
        if (nowMonth < 3) {
            quarterStartMonth = 0;
        }
        if (2 < nowMonth && nowMonth < 6) {
            quarterStartMonth = 3;
        }
        if (5 < nowMonth && nowMonth < 9) {
            quarterStartMonth = 6;
        }
        if (nowMonth > 8) {
            quarterStartMonth = 9;
        }
        return quarterStartMonth;
    }

    //获得某月的天数
    function getMonthDays(nowYear, myMonth) {
        var monthStartDate = new Date(nowYear, myMonth, 1);
        var monthEndDate = new Date(nowYear, myMonth + 1, 1);
        var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    }

    function onComDataselChanged() {
        var obj = mini.get("comdatasel");
        var nowDate = new Date();
        switch (obj.getValue()) {
            case "0":  //当天
                mini.getbyName("FDate1").setValue(nowDate);
                mini.getbyName("FDate3").setValue(nowDate);
                onValueChanged3();
                break;
            case "1":  //本月
                var currentMonth = nowDate.getMonth();
                var dtStart = new Date(nowDate.getFullYear(), currentMonth, 1);
                var nextMonth = ++currentMonth;
                var nextMonthFirstDay = new Date(nowDate.getFullYear(), nextMonth, 1);
                var oneDay = 1000 * 60 * 60 * 24;
                var dtEnd = new Date(nextMonthFirstDay - oneDay);
                mini.getbyName("FDate1").setValue(dtStart);
                mini.getbyName("FDate3").setValue(dtEnd);
                onValueChanged3();
                break;
            case "2":  //上月
                var currentMonth = nowDate.getMonth();
                var preMonth = --currentMonth;
                var dtStart = new Date(nowDate.getFullYear(), preMonth, 1);
                var nextMonth = ++preMonth;
                var nextMonthFirstDay = new Date(nowDate.getFullYear(), nextMonth, 1);
                var oneDay = 1000 * 60 * 60 * 24;
                var dtEnd = new Date(nextMonthFirstDay - oneDay);
                mini.getbyName("FDate1").setValue(dtStart);
                mini.getbyName("FDate3").setValue(dtEnd);
                onValueChanged3();
                break;
            case "3":  //昨天
                var nowDate = new Date();
                nowDate.setDate(nowDate.getDate() - 1);

                mini.getbyName("FDate1").setValue(nowDate);
                mini.getbyName("FDate3").setValue(nowDate);
                onValueChanged3();
                break;
            case "4":  //本周
                var nowDate = new Date(); //当前日期
                var nowDay = nowDate.getDate(); //当前日
                var nowMonth = nowDate.getMonth(); //当前月
                var nowYear = nowDate.getFullYear(); //当前年
                var nowDayOfWeek = nowDate.getDay(); //今天本周的第几天
                var dtStart = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
                var dtEnd = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
                mini.getbyName("FDate1").setValue(dtStart);
                mini.getbyName("FDate3").setValue(dtEnd);
                onValueChanged3();
                break;
            case "5": //本季度
                var nowYear = nowDate.getFullYear(); //当前年
                var nowMonth = nowDate.getMonth(); //当前月

                var dtStart = new Date(nowYear, getQuarterStartMonth(nowMonth), 1);
                mini.getbyName("FDate1").setValue(dtStart);
                var quarterEndMonth = getQuarterStartMonth(nowMonth) + 2;
                var dtEnd = new Date(nowYear, quarterEndMonth, getMonthDays(nowYear, quarterEndMonth));

                //mini.getbyName("FDate1").setValue(dtStart);
                mini.getbyName("FDate3").setValue(dtEnd);
                onValueChanged3();
                break;
            case "6": //本年度
                var nowYear = nowDate.getFullYear(); //当前年
                var dtStart = new Date(nowYear, 0, 1);
                var dtEnd = new Date(nowYear, 11, 31);
                mini.getbyName("FDate1").setValue(dtStart);
                mini.getbyName("FDate3").setValue(dtEnd);
                onValueChanged3();
                break;
        }
    }
</script>